package com.aopcinemas.visitor.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.commons.lang3.StringUtils;

import com.aopcinemas.cinema.model.Movie;
import com.aopcinemas.cinema.model.Movie.AgeConstraint;
import com.aopcinemas.cinema.model.Movie.Genre;
import com.aopcinemas.common.ui.UIUtils;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.model.Campaign.Type;
import com.aopcinemas.visitor.ontology.GetCampaign;

public class CampaingSearchAgentDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private VisitorAgent agent;

    public CampaingSearchAgentDialog(JFrame owner, VisitorAgent agent) {
        super(owner, true);
        this.agent = agent;

        JPanel panel = new JPanel();

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 3, 3, 3);
        panel.setLayout(layout);

        final MovieSearchPanel movieSearchPanel = new MovieSearchPanel();
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(movieSearchPanel, constraints);
        panel.add(movieSearchPanel);

        JLabel label = new JLabel("Group visit type: ");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        movieSearchPanel.layout.setConstraints(label, constraints);
        movieSearchPanel.add(label);

        JComboBox campaingnType = new JComboBox(Type.values());
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        movieSearchPanel.layout.setConstraints(campaingnType, constraints);
        movieSearchPanel.add(campaingnType);

        JPanel buttonPanel = new JPanel();

        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                GetCampaign getCampaign = new GetCampaign();

                getCampaign.setStartTime(UIUtils.mergeDateAndTime(movieSearchPanel.startDate.getDate(), (Date) movieSearchPanel.startTime.getValue()));
                getCampaign.setEndTime(UIUtils.mergeDateAndTime(movieSearchPanel.endDate.getDate(), (Date) movieSearchPanel.endTime.getValue()));

                Movie movie = new Movie();
                movie.setName(movieSearchPanel.movieName.getText());
                movie.setGenre((Genre) movieSearchPanel.genreList.getSelectedItem());
                movie.setAgeConstraint((AgeConstraint) movieSearchPanel.ageConstraintList.getSelectedItem());
                getCampaign.setMovie(movie);

                if (StringUtils.isNotBlank(movieSearchPanel.period.getText())) {
                    getCampaign.setPeriod(Long.parseLong(movieSearchPanel.period.getText()));
                }

                CampaingSearchAgentDialog.this.agent.setGetCampaign(getCampaign);
            }
        });
        buttonPanel.add(addButton);

        JButton removeButton = new JButton("Remove");
        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CampaingSearchAgentDialog.this.agent.setGetCampaign(null);
            }
        });
        buttonPanel.add(removeButton);

        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(buttonPanel, constraints);
        panel.add(buttonPanel);

        getContentPane().add(panel);

        setSize(400, 350);
    }
}
